import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.security.AccessControlException;
import javax.swing.BoxLayout;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * A JPanel that allows the user to specify their name
 * 
 */
public class CreateUserPanel extends JPanel implements ActionListener 
{
	private JTextField nameEntry; 
	private String[] restrictions;
	private JLabel message;
	
	public CreateUserPanel()
	{
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		message= new JLabel("");
		message.setAlignmentX(Component.CENTER_ALIGNMENT);
		JLabel label= new JLabel("Please Enter Your Name");
		nameEntry= new JTextField("", 20);
		nameEntry.addActionListener(this);
		nameEntry.setMaximumSize(new Dimension(160, 20));
		nameEntry.setAlignmentX(Component.CENTER_ALIGNMENT);
		label.setAlignmentX(Component.CENTER_ALIGNMENT);
		add(new JLabel("       "));
		add(new JLabel("       "));
		add(new JLabel("       "));
		add(message);
		add(label);
		add(nameEntry);
	}
	
	/**
	 * Creates a new CreateUserPanel with a array of names that can't be used
	 * @param restrictions the Array of Strings that can't be used as a name
	 */
	public CreateUserPanel(String[] restrictions)
	{
		this();
		this.restrictions= restrictions;
	}
	
	/**
	 *  if the user inputs a valid name the method will create a new user with
	 *  that name as the current user and replace itself with a SettingsPanel
	 */
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(nameEntry.getText()==null||nameEntry.getText().equals("")) 
		{
			message.setText("Please Enter Your Name");
			return;
		}
		char[] chars= nameEntry.getText().toCharArray();
		for(int i=0; i<chars.length; i++)
		{
			if(Character.isLetterOrDigit(chars[i])==false)
			{
				message.setText("Name May Only Contain Letters and Numbers");
				return;
			}
		}
		if(restrictions!=null)
		{
			for(int k=0; k<restrictions.length; k++)
			{
				if(nameEntry.getText().equalsIgnoreCase(restrictions[k]))
				{
					message.setText("That Name Is Already Taken");
					return;
				}
			}
		}
		Global.currentUser= new User(nameEntry.getText());
		setVisible(false);
		Component ancestor= this.getTopLevelAncestor();
		if(Global.isApplet()) 
		{
			JApplet applet= (JApplet)(ancestor);
			applet.setContentPane(new SettingsPanel());
		}
		else 
		{
			FileWriter file;
			try
			{
				file= new FileWriter(nameEntry.getText()+".dat");
				file.close();
			}
			// if the name is illegal make the user input another
			catch(IOException ex)
			{
				message.setText("Your Name Must Be A Valid File Name");
				return;
			}
			// if the program can't write to files just continue
			catch(AccessControlException ex)
			{
				
			}
			JFrame frame= (JFrame)(ancestor);
			frame.setContentPane(new SettingsPanel());
			frame.pack();
		}
	}

}
